package com.zadtek.sharkRider.activity.adapter;

import java.util.List;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.SharedPreferences;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.zadtek.sharkRider.R;
import com.zadtek.sharkRider.activity.common.Common;
import com.zadtek.sharkRider.activity.common.Const;
import com.zadtek.sharkRider.activity.data.OrderNewHelpData;
import com.zadtek.sharkRider.views.OnChanged;

/**
 * 新任务列表
 * @author kywwl
 *
 */
public class OrderNewHelpAdapter extends CommonAdapter<OrderNewHelpData> {
	private OnChanged listener;

	public OrderNewHelpAdapter(Context context, List<OrderNewHelpData> datas,
			int layoutId) {
		super(context, datas, layoutId);
		// TODO Auto-generated constructor stub
	}

	@SuppressLint("NewApi")
	@Override
	public void convert(final ViewHolders holder, final OrderNewHelpData t) {
		ImageView newtask_order_img =holder.getView(R.id.newtask_order_img);
		TextView newtaskmomey = holder.getView(R.id.newtask_money);
		TextView newtasktime = holder.getView(R.id.newtask_time);
		LinearLayout show_layout =holder.getView(R.id.show_layout);
		
		TextView newtask_goods_name =holder.getView(R.id.newtask_goods_name);
		TextView newtask_goods_address =holder.getView(R.id.newtask_goods_address);
		TextView help_goods_distance =holder.getView(R.id.help_goods_distance);
		TextView newtask_goods_distance =holder.getView(R.id.newtask_goods_distance);
		TextView newtask_goods_getaddress = holder
				.getView(R.id.newtask_goods_getaddress);
		Button submit = holder.getView(R.id.submit);
		
		newtask_goods_name.setText(t.getTitle());
		newtask_goods_address.setText(t.getGet_address());
		LinearLayout newhelp_layout = holder.getView(R.id.newhelp_layout);
		if (t.getGet_address().length() > 0) {
			newtask_goods_address.setVisibility(View.VISIBLE);
		} else {
			newtask_goods_address.setVisibility(View.GONE);
		}
		if(t.getType().equals("buy")){
			newtask_order_img.setImageResource(R.drawable.gou);
			submit.setText("帮你买");
		}else if(t.getType().equals("get")){
			newtask_order_img.setImageResource(R.drawable.getgoods);
			submit.setText("帮你取");
		}else if(t.getType().equals("send")){
			newtask_order_img.setImageResource(R.drawable.fa);
			submit.setText("帮你送");
		}
		
		LinearLayout juli_layout =holder.getView(R.id.juli_layout);//送货距离
		LinearLayout juli_layout01 =holder.getView(R.id.juli_layout01);//取货距离
		if (!t.getLocation_get().equals("0")) {
			help_goods_distance.setText(t.getLocation_get()+"Km");
			juli_layout01.setVisibility(View.VISIBLE);
		}else{
			juli_layout01.setVisibility(View.GONE);
		}
		if (!t.getLocation_send().equals("0")) {
			newtask_goods_distance.setText(t.getLocation_send()+"Km");
			juli_layout.setVisibility(View.VISIBLE);
		}else{
			juli_layout.setVisibility(View.GONE);
		}
		newtask_goods_getaddress.setText(t.getSend_address());
		
		TextView getgpsaddr = holder.getView(R.id.getgpsaddr);
		newtaskmomey.setText("￥" + t.getPercentage());
		if (!t.getHelp_addtime().equals("") || t.getHelp_addtime() != null) {
			newtasktime.setText(Common.getStrTime(t.getHelp_addtime()));
		}
		
		try {
			String address_road =t.getMap_location();
			if (address_road ==null || address_road.equals("") || address_road.equals("null")) {
				getgpsaddr.setVisibility(View.GONE);
			}else{
				getgpsaddr.setText(address_road); // 根据经纬度反查地址
				getgpsaddr.setVisibility(View.VISIBLE);
			}
		} catch (Exception e) {
		}

		

		submit.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				
				SharedPreferences sp = mContext.getSharedPreferences(
						Const.PREF_NAME, Context.MODE_PRIVATE);
				String Uid = sp.getString("uid", "");
				if (null != Uid && !Uid.equals("")) {
					listener.getHelpChoiceData(t.getHelp_order_id(), Uid,holder.getPositon());
				}
			}
		});
	}

	public void setHelpOnChanged(OnChanged listener) {
		this.listener = listener;
	}
	
}
